package cmj.cloudmusic.fragment.Discover;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cmj.cloudmusic.R;
import cmj.cloudmusic.adapter.TabFragmentAdapter;

/**
 * Created by 王虎 on 2016/12/10.
 */
public class DiscoverFragment extends Fragment {
    private View view;

    private TabLayout tabLayout;
    private List<String> tabTitles;

    private ViewPager viewPager;
    private List<Fragment> fragments;
    private FragmentPagerAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
            return view;
        }
        view=inflater.inflate(R.layout.discover_fragment,null);
        tabLayout= (TabLayout)view.findViewById(R.id.tablayout);
        viewPager= (ViewPager) view.findViewById(R.id.viewpager);
        initTab();
        initVP();
        return view;
    }

    private void initTab(){


        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabTitles=new ArrayList<>();
        tabTitles.add("个性推荐");
        tabTitles.add("歌单");
        tabTitles.add("主播电台");
        tabTitles.add("排行榜");

        tabLayout.addTab(tabLayout.newTab().setText("个性推荐"), true);
        tabLayout.addTab(tabLayout.newTab().setText("歌单"));
        tabLayout.addTab(tabLayout.newTab().setText("主播电台"));
        tabLayout.addTab(tabLayout.newTab().setText("排行榜"));
    }
    private void initVP(){
        D_bangFragment d_bangFragment=new D_bangFragment();
        D_gedanFragment d_gedanFragment= new D_gedanFragment();
        D_gexingFragment d_gexingFragment=new D_gexingFragment();
        D_redioFragment d_redioFragment=new D_redioFragment();
        fragments=new ArrayList<>();
        fragments.add(d_gexingFragment);
        fragments.add(d_gedanFragment);
        fragments.add(d_redioFragment);
        fragments.add(d_bangFragment);
        adapter=new TabFragmentAdapter(getChildFragmentManager(),fragments,tabTitles);
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
    }
}
